﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyCommerce.ManagementBase.Common.Base.Common;
using Innova.Microfinanzas.BusinessEntities;
using Innova.Microfinanzas.BusinessEntities.SystemEnums;
using MyCommerce.Presentation.Stores.IViews;
using MyCommerce.Presentation.Stores.Presenters;
using Innova.FoundationFramework.DataTypes;

namespace MyCommerce.Windows.Stores.Views
{
    public partial class ProductPriceFillView : BaseForm, IProductPriceFill
    {
        private ProductPriceFillPresenter pPresenter;

        public ProductPriceFillView()
        {
            InitializeComponent();
            pPresenter = new ProductPriceFillPresenter(this);
        }

        #region IEmployeeFillView Members

        public long IdItemEdit { get; set; }

        public OptionSelect OptionViewSelect { get; set; }

        public List<Classifier> ColMoney
        {
            set
            {
                cmbMoney.DisplayMember = "Name";
                cmbMoney.ValueMember = "IdClassifier";
                cmbMoney.DataSource = value;
            }
        }

        public List<Supplier> ColSupplier
        {
            set
            {
                cmbSupplier.DisplayMember = "Name";
                cmbSupplier.ValueMember = "IdSupplier";
                cmbSupplier.DataSource = value;
            }
        }

        public List<Product> ColProduct
        {
            set
            {
                cmbProduct.DisplayMember = "Model";
                cmbProduct.ValueMember = "IdProduct";
                cmbProduct.DataSource = value;
            }
        }

        public ProductPrice Get()
        {
            return ProductPrice.CreateProductPrice(0,
                (long)cmbProduct.SelectedRow.Cells["IdProduct"].Value,
                Convert.ToDecimal(txtPurchasePrice.Value),
                Convert.ToDecimal(txtSalePrice.Value),
                (long)cmbMoney.SelectedValue,
                (long)cmbSupplier.SelectedValue,
                true,
                DateTime.Now, Convert.ToDecimal(fieldOtherPurchasePrice.Value));
        }

        public void Set(ProductPrice objProductToLoad)
        {
            IdItemEdit = objProductToLoad.IdProductPrice;
            if (String.IsNullOrEmpty(cmbMoney.ValueMember) == false
                && objProductToLoad.IdcMoney > 0)
                cmbMoney.SelectedValue = objProductToLoad.IdcMoney;
            if (String.IsNullOrEmpty(cmbSupplier.ValueMember) == false
                && objProductToLoad.IdSupplier > 0)
                cmbSupplier.SelectedValue = objProductToLoad.IdSupplier;
            if (String.IsNullOrEmpty(cmbProduct.ValueMember) == false
                && objProductToLoad.IdProduct > 0)
                cmbProduct.Value = objProductToLoad.IdProduct;
            txtPurchasePrice.Value = objProductToLoad.PurchasePrice;
            txtSalePrice.Value = objProductToLoad.SalePrice;
            fieldOtherPurchasePrice.Value = objProductToLoad.OtherPurchasePrice;
        }

        public void CloseAndSetResult(bool result)
        {
            if (result)
                Context.ShowInformationMessageBox("La información se guardó correctamente.", "Creación/Edición de productos.");
            Result = result;
            Close();
        }

        public event GenericEventHandler<ProductPrice> OnLoadView;

        public event EventHandler OnSave;

        public event EventHandler OnCancel;

        #endregion

        private bool ValidateView()
        {
            bool vResult = true;
            StringBuilder message = new StringBuilder();

            if (string.IsNullOrEmpty(txtSalePrice.Text.Trim()))
            {
                vResult = false;
                message.AppendLine("Debe ingresar un precio de venta.");
            }

            if (string.IsNullOrEmpty(txtPurchasePrice.Text.Trim()))
            {
                vResult = false;
                message.AppendLine("Debe ingresar un precio de compra.");
            }
            
            if (!vResult)
            {
                Context.ShowErrorMessageBox(message.ToString(), "Validación de datos.");
            }

            return vResult;
        }

        private void EmployeeFillView_Load(object sender, EventArgs e)
        {
            ProductPrice objProductPrice = null;
            OptionViewSelect = OptionSelect.Create;
            if (Parameter != null)
            {
                objProductPrice = (ProductPrice)Parameter;
                OptionViewSelect = OptionSelect.Edit;
            }
            if (OnLoadView != null)
                OnLoadView(this, new GenericEventArgs<ProductPrice>(objProductPrice));
        }

        private void tsCancelButtonToolStrip_Click(object sender, EventArgs e)
        {
            if (OnCancel != null)
                OnCancel(this, new EventArgs());
        }

        private void tsSaveButtonToolStrip_Click(object sender, EventArgs e)
        {
            try
            {
                if (ValidateView())
                {
                    if (OnSave != null)
                        OnSave(this, new EventArgs());
                }
            }
            catch (Exception ex)
            {
                Context.HandleException(ex);
            }
        }

    }
}
